<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-10-20 09:33:57
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Service;
use App\Repositories\AttachmentRepository;
use App\Repositories\MpcOtherDetailRepository;
use App\Repositories\MpcOtherRepository;

/**
 * @property MpcOtherRepository $repository
 * @property MpcOtherDetailRepository $Detail
 * @property AttachmentRepository $att
 */

class OtherService extends Service
    {


    public function boot(MpcOtherRepository $Repository, MpcOtherDetailRepository $detail, AttachmentRepository $att)
        {
        $this->repository = $Repository;
        $this->Detail     = $detail;
        $this->att        = $att;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function info($requset)
        {
        return $this->repository->condition($requset)->getOne();
        }

    public function details()
        {
        return $this->Detail->condition(request())->getGrid();
        }
    protected function save($request)
        {
        $ReqData = $request->toArray();

        $data = $this->repository->whereId($request->input("id"))->first();
        if ($data && $data->status_id > 0) {
            fail("已审核单据禁止编辑！");
            }

        $ReqData['quantity']           = 0;
        $ReqData['auxiliary_quantity'] = 0;
        
        foreach ($ReqData['details'] as $key => &$value) {
            $value['quantity']           = floatval($value['quantity']);
            $value['auxiliary_quantity'] = empty($value['auxiliary_quantity'])? 0 : floatval($value['auxiliary_quantity']);
            $ReqData['quantity']           += floatval($value['quantity']);
            $ReqData['auxiliary_quantity'] += empty($value['auxiliary_quantity'])? 0 : floatval($value['auxiliary_quantity']);
        }

        $gdata = $this->repository->autosave($ReqData);
        $key   = 1;
        $gdata->details->filter(function ($v) use ($gdata, &$key)
            {
            $v->customer_id = $v->iscustomer == 1 ? $v->company_id : 0;
            $v->batchno     = $gdata->code;
            $v->remark      = $gdata->remark;
            $v->save();
            });
        }
    protected function audit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！");
                auditSave($v);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        $deltail->addStorage("其他入库单", $v->code);
                        }
                );
                });
        }
    protected function noaudit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 1)
                    fail("单据:[{$v->code}]未审核！");
                auditSave($v, false);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        $deltail->noaddStorage();
                        }
                );

                $v->save();
                });
        }

    protected function delete($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！无法删除");
                $v->details->filter(
                    function ($detail)
                        {
                        $detail->delete();
                        }
                );
                $v->delete();
                });
        }
    protected function auditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->audit($ids);
        }
    protected function noauditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->noaudit($ids);
        }
    protected function deleteItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->delete($ids);
        }

    }